<?php
global $AppUI, $project_id, $df, $canEdit, $m, $tab, $canViewTaskFee, $canCreateTasks, $hasRoleFfifaMana;

require_once($AppUI->getModuleClass('companies'));
require_once($AppUI->getModuleClass('suppliers'));
require_once($AppUI->getModuleClass('items'));
require_once($AppUI->getModuleClass('tasks'));
require_once($AppUI->getModuleClass('projects'));
require_once($AppUI->getMasterDataClass('mdtype'));
require_once($AppUI->getMasterDataClass('mdstatus'));

$df = $AppUI->getPref('SHDATEFORMAT');

if (isset($_POST['resetAction'])) {
	$AppUI->unsetPOST('TASK_POST');
}
else {
	if (count($_POST) > 0) {
		$AppUI->savePOST('TASK_POST');
	}
	else {
		$_POST = $AppUI->getPOST('TASK_POST');
	}
}

$page_offset = intval( dPgetParam( $_GET, 'page_offset', 0 ) );
//$page_limit = intval( dPgetParam( $_POST, 'page_limit', $AppUI->getPref('PAGELIMIT', 5) ) );
$page_limit = intval( dPgetParam( $_POST, 'page_limit', 999999));
$status_id = intval( dPgetParam( $_POST, 'status_id', 0 ) );
$item_type = intval( dPgetParam( $_POST, 'item_type', 0 ) );
$customer = intval( dPgetParam( $_POST, 'customer', 0 ) );
$project_shipping_type = intval( dPgetParam( $_POST, 'project_shipping_type', 0 ) );
$keyword = dPgetParam( $_POST, 'keyword', '' );
$project_name = dPgetParam( $_POST, 'project_name', '' );
$project_code = dPgetParam( $_POST, 'project_code', '' );
$from_date = dPgetParam( $_POST, 'from_date', '' );
$to_date = dPgetParam( $_POST, 'to_date', '' );
//do search
$q = new DBQuery();
$q->dropTemp('pitems, wunit, vunit, qunit, amounts');
$q->exec();
$q->clear();

$q->createTemp('pitems');
$q->addTable('items');
if ($item_type > 0) {
	$q->addWhere("item_type1 = $item_type");
}
$q->exec();
$q->clear();

$q->createTemp('otherexpense');
$q->addTable('task_log');
$q->addQuery('task_log_task, SUM(task_log_amount) as sum_amount');
$q->addWhere('task_log_expense = 3');
$q->addGroup('task_log_task');
$q->exec();
$q->clear();

//main query for tasks
$q->addTable('tasks');
$q->innerJoin('projects', 'pro', 'project_id = task_project');
$q->innerJoin('pitems', 'pi', 'item_id = task_item');
$q->leftJoin('otherexpense', 'oe', 'task_log_task = task_id ');
$q->leftJoin('quotations', 'q', 'task_quotation = quote_id ');
$q->addWhere('task_project = ' . $project_id);
$q->addWhere('((task_quotation_version > 0 AND (task_quotation_version < quote_version OR quote_completed < 1)) or task_quotation IS NULL or task_quotation = 0 OR task_percent_complete < 100 OR (sum_amount IS NOT NULL AND sum_amount > 0))');
$q->addOrder('task_start_date desc, task_name');
$qTotal = $q->prepare();
$q->addQuery('distinct tasks.*, oe.*, q.*');
$tasks = $q->loadList();
// Load Object Status
$objMD = new CMdStatus();
$statList = $objMD->getStatusList('Reference');

$objMD = new CMdType();
$itemtypeList = $objMD->getTypeList('ItemType1');

$objMD = new CCompany();
$cusList = $objMD->getAllowedRecords($AppUI->user_id, 'company_id, company_name', 'company_name');
$cusList = arrayMerge(array('0'=>"" . $AppUI->_(''). ""), $cusList);
$q->dropTemp('pitems, wunit, vunit, qunit, amounts, otherexpense');
?>
<table id="idTasksList" class="list" cellspacing="0" cellpadding="2" border="0" width="100%">
		<tr class="<?=($i % 2 == 0) ? "second" : "first" ?>">
			<th >&nbsp;</th>
			<th >&nbsp;</th>
			<th align="right"><?=$AppUI->_('%')?></th>
			<th align="center"><?=$AppUI->_('Name')?></th>
			<th align="center"><?=$AppUI->_('Broken quotation')?></th>
			<th align="center"><?=$AppUI->_('Link quotation')?></th>
			<th align="center"><?=$AppUI->_('Percent complete')?></th>
			<th align="center"><?=$AppUI->_('Other expenses')?></th>
		</tr>
<?php
if (is_array($tasks) && count($tasks) > 0) {
	$totalRows = 0;
	$qTotal = str_ireplace('select *', 'select count(*) as total_rows', $qTotal);
	$tmp = db_loadList($qTotal);
	$totalRows = $tmp[0]['total_rows'];

	require_once($AppUI->getLibraryClass('codeigniter/pagination'));
	$pagination = new CI_Pagination();
	$config = array(
		'base_url' => '?m=projects&tab=1',
		'total_rows' => $totalRows,
		'per_page' => $page_limit

	);
	$pagination->initialize($config);
	$i = 0;
	?>

	<script type="text/javascript">
		function submitForm(offset) {
			document.reportFrm.offset.value = offset;
			document.reportFrm.submit();
		}
	</script>
	<?
	$sumAmounts = 0.00;
	$$taskFee = 0.00;
	foreach ($tasks as $row) {
		$sumAmounts += $row['sum_amount'];
		$taskFee += $row['task_fee'] > 0 ? $row['task_fee'] : 0;
		$i++;
		$start_date = new CDate($row['task_start_date']);
		$end_date = new CDate($row['task_end_date']);
		$my_cc_task = $row['task_costcenter'] == $AppUI->user_costcenter;
		?>
		<tr class="<?=($i % 2 == 0) ? "second" : "first" ?>" onClick="showTask(<?=$row['task_id']?>)">
			<td align='center'>
				<?if($row->task_is_complete < 1 && ($hasRoleFfifaMana || $my_cc_task)){ ?>
					<a href="?m=timecard&a=vw_newlog&project_id=<?=$row['task_project']?>&task_id=<?=$row['task_id']?>"><image src="images/add_log.gif" border="0" alt="Add log"/></a>
				<?} else {?>
					<image src="images/noadd_log.gif" border="0" alt="No Add log"/>
				<?}?>

			</td>
			<td align='center'>
				<?if($row->task_is_complete < 1 &&($hasRoleFfifaMana || $my_cc_task)){ ?>
					<a href="?m=tasks&a=simpleaddedit&task_id=<?=$row['task_id']?>"><image src="images/edit.gif" border="0" alt="Edit"/></a>
				<?} else {?>
					<a href="?m=tasks&a=view&task_id=<?=$row['task_id']?>"><image src="images/noedit_task.gif" border="0" alt="No Edit"/></a>
				<?}?>
			</td>
			<td align='right'><?=$row['task_is_complete'] == 0 ? (@$row['task_percent_complete'] . '%') : '<font color="Green">Completed</font>'?></td>
			<td ><?=$row['task_name']?></td>
			<td align="center">
			<?php
				if (intval($row['task_quotation']) > 0 && (intval($row['task_quotation_version']) < $row['quote_version'] || $row['quote_completed'] < 1)) {
					echo dPshowImage("images/red.png");
				}
			?>
			</td>
			<td align="center">
			<?php
				//(task_quotation IS NULL or task_quotation = 0 or task_percent_complete < 100 or (sum_amount IS NOT NULL AND sum_amount > 0))');
				if (!isset($row['task_quotation']) || $row['task_quotation'] < 1) {
					echo dPshowImage("images/red.png");
				}
			?>
			</td>
			<td align="center">
			<?php
				//(task_quotation IS NULL or task_quotation = 0 or task_percent_complete < 100 or (sum_amount IS NOT NULL AND sum_amount > 0))');
				if ($row['task_percent_complete'] < 100) {
					echo dPshowImage("images/red.png");
				}
			?>
			</td>
			<td align="center">
			<?php
				//(task_quotation IS NULL or task_quotation = 0 or task_percent_complete < 100 or (sum_amount IS NOT NULL AND sum_amount > 0))');
				if (isset($row['sum_amount']) &&  $row['sum_amount'] > 0) {
					echo dPshowImage("images/red.png");
				}
			?>
			</td>
		</tr>
		<?
	}
	?>
	</table>

	<?
} else {
	showEmptyRow('1', 'second', 'Not found any tasks', '100%');
}

?>
</table>
<script>
	Calendar.enabled = true;

	Table("idTasksList").setNormalHover(1);

	function showTask(id)
	{
		location.href = "index.php?m=tasks&a=view&task_id=" + id;
	}

	function doSearch(frm) {
		frm.action = '?m=projects&tab=1&suppressHeaders=0';
		frm.submit();
	}

	function doExport(frm) {
		//frm.action = '?m=reports&a=do_r_reference&suppressHeaders=1';
		//frm.submit();
	}
</script>
